package com.onlyxiahui.common.action.description.util;

import java.lang.annotation.Annotation;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;

/**
 * Description <br>
 * Date 2020-01-08 09:59:12<br>
 * 
 * @author XiaHui [onlovexiahui@qq.com]<br>
 * @since 1.0.0
 */

public class ActionAnnotationUtil {

	public static boolean hasAnnotation(String name, Annotation[] as) {
		boolean has = false;
		if (null != name && null != as) {
			for (Annotation a : as) {
				if (name.equals(a.annotationType().getName())) {
					return true;
				}
			}
		}
		return has;
	}

	public static Annotation getAnnotation(String name, Annotation[] as) {
		Annotation has = null;
		if (null != name && null != as) {
			for (Annotation a : as) {
				if (name.equals(a.annotationType().getName())) {
					return a;
				}
			}
		}
		return has;
	}

	public static Object getValue(String name, String methodName, Annotation[] as) {
		Object o = null;
		Annotation a = getAnnotation(name, as);
		if (null != a) {
			return getValue(methodName, a);
		}
		return o;
	}

	public static Object getValue(String methodName, Annotation a) {
		Object o = null;
		if (null != a) {
			Method[] methods = a.annotationType().getMethods();
			if (null != methods && methods.length > 0) {
				for (Method method : methods) {
					if (method.getName().equals(methodName)) {
						try {
							return method.invoke(a);
						} catch (IllegalAccessException e) {
							e.printStackTrace();
						} catch (IllegalArgumentException e) {
							e.printStackTrace();
						} catch (InvocationTargetException e) {
							e.printStackTrace();
						}
					}
				}
			}
		}
		return o;
	}
}
